package co.gov.medellin.ssti.sub.paoyer.administration.web.action;

import java.io.IOException;
import java.util.Collection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import co.gov.medellin.ssti.sub.paoyer.administration.AdministrationMngr;
import co.gov.medellin.ssti.sub.paoyer.administration.model.vo.UserVO;
import co.gov.medellin.ssti.sub.paoyer.administration.web.form.UserForm;

public class GenerarReporteUsuariosAction extends Action {
	/**
	 * This is the main action called from the Struts framework.
	 * 
	 * @param mapping
	 *            The ActionMapping used to select this instance.
	 * @param form
	 *            The optional ActionForm bean for this request.
	 * @param request
	 *            The HTTP Request we are processing.
	 * @param response
	 *            The HTTP Response we are processing.
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {

		UserForm userForm = (UserForm) form;
		String strarea = request.getParameter("code");
		AdministrationMngr delegate = new AdministrationMngr();

		if (request.getParameter("search") != null) {
			UserVO user = new UserVO();
			user.setName(userForm.getName());
			user.setDescription(userForm.getName());
			user.setUserGroupCode(userForm.getUserGroupCode());

			if (strarea.equals("")) {
				user.setCodeArea(null);
			} else {
				user.setCodeArea(new Long(strarea));
			}
			user.setUse(userForm.getUse());
			Collection<UserVO> userList = delegate
					.findUserByCritereaAdmin(user);
			if (userList != null && userList.size() > 0) {
				request.setAttribute("userList", userList);
			} else {
				request.removeAttribute("userList");
			}

		}

		return mapping.findForward("success");
	}
}
